//
//  PersonEditTableViewCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/6.
//  Copyright © 2016年 game. All rights reserved.
//

#import "PersonEditTableViewCell.h"

#define cellHeightCount (SCREEN_HEIGHT*0.2)+15+15

@interface PersonEditTableViewCell ()

@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *linHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *linWidth;

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnBackHeight;

/**
 *  删除
 */
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;


/**
 *  修改
 */
@property (weak, nonatomic) IBOutlet UIButton *repairBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *repirWidth;
/**
 * 重发
 */
@property (weak, nonatomic) IBOutlet UIButton *anewBtn;
@end

@implementation PersonEditTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}

- (void)awakeFromNib {
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = 0;
    
    self.btnBackHeight.constant = SCREEN_HEIGHT*0.07;
    
    self.linHeight.constant = 0.6;
    self.linWidth.constant = self.linHeight.constant;
    
    self.title.font = [UIFont boldSystemFontOfSize:SCREEN_WIDTH*0.05];
    
    self.date.font = [UIFont boldSystemFontOfSize:SCREEN_WIDTH*0.035];
    
    self.repirWidth.constant = SCREEN_WIDTH/3;                
}


-(void)setModel:(PersonEditModel *)model{

    _model = model;
    
    self.title.text = _model.name;
    self.date.text = _model.time;
    
    self.type = _model.type;
    self.Id = _model.Id;
    
    self.isyujud = _model.isyujud;
    
    [self.backView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
        if (_model.images.count!=0) {
            CGFloat xd = 10;
            CGFloat ww = (SCREEN_WIDTH-4*xd)/3;
            self.height = ww+ cellHeightCount;

        }else{
        
            
            UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 0)];
            textView.text = _model.text;
            
            textView.font = font(SCREEN_WIDTH*0.044);
            CGRect textViewFrame = textView.frame;
            CGSize textSize = [textView sizeThatFits:CGSizeMake(CGRectGetWidth(textViewFrame), 1000.0f)];
            textViewFrame.size.height = textSize.height;
            
            self.height = textViewFrame.size.height+ cellHeightCount-20;

        }
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    if (_model.images.count!=0) {//如果有图片则显示图片 否则显示文字
        NSInteger count = _model.images.count;
        if (count>3) {
            count=3;
        }
        
        for (int i=0; i<count; i++) {
            
            UIButton *imageview = [[UIButton alloc]init];
            CGFloat xd = 10;
            CGFloat ww = (SCREEN_WIDTH-4*xd)/3;
            CGFloat xx = xd +(xd + ww)*i;
            
            imageview.frame = CGRectMake(xx, 10, ww, ww);
            
            [imageview setImage:[UIImage imageWithData: _model.images[i]] forState:UIControlStateNormal];
            imageview.imageView.contentMode  = UIViewContentModeScaleAspectFill;
            
            [self.backView addSubview:imageview];
            
    
        }
        
    } else{
        
        UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 0)];
        textView.text = _model.text;
        textView.scrollEnabled = NO;
        textView.font = font(SCREEN_WIDTH*0.044);
        CGRect textViewFrame = textView.frame;
        CGSize textSize = [textView sizeThatFits:CGSizeMake(CGRectGetWidth(textViewFrame), 1000.0f)];
        textViewFrame.size.height = textSize.height;
        textView.frame = textViewFrame;
        textView.userInteractionEnabled = NO;
        textView.editable = NO;
        
        [self.backView addSubview:textView];
        
    
    }


    
    
    
}

/**
 *  删除
 */
- (IBAction)deleteClick:(id)sender {
    
    [self.deleget deleteBtnClickWhithCell:self];
    
    
}

/**
 *  修改
 */
- (IBAction)repairClick:(id)sender {
    
    [self.deleget repairBtnClickWhithCell:self];
}


/**
 *  重发
 */
- (IBAction)anewClick:(id)sender {
    [self.deleget anewBtnClickWhithCell:self];
    
}


@end
